import { Controller } from "../../mvc/core/Controller";
import { ActivityPrivilegeIcon } from "../view/activityPrivilege/ActivityPrivilegeIcon";
import { IView } from "../../mvc/interfaces/IView";
import { ActivityPrivilegeModule } from "../../../modules/ActivityPrivilegeModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { ClockManager } from "../manager/ClockManager";
import { IActivityPrivilegeModule } from "../../../modules/interfaces/IActivityPrivilegeModule";
type int = number;
//class ActivityPrivilegeController
    
    export  class ActivityPrivilegeController extends Controller
    {
       

        public get icon(): ActivityPrivilegeIcon
        {
            if (!this._icon) 
            {
                this._icon = new ActivityPrivilegeIcon();
                this._icon.updateIconInfo();
            }
            return this._icon;
        }

        protected /* override */ initView(): IView
        {
            if (!this._module) 
            {
                this._module = new ActivityPrivilegeModule();
                this._module.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStageHandler);
                _view = this._module;
            }
            return this._module;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.ActivityPrivilegeOpen, this.onActivityPrivilegeOpenHandler);
            Dispatcher.addEventListener(EventName.ActivityPrivilegeView, this.onActivityPrivilegeViewHandler);
            Dispatcher.addEventListener(EventName.ActivityPrivilegeBuy, this.onActivityPrivilegeBuyHandler);
            Dispatcher.addEventListener(EventName.ActivityPrivilegeInfoUpdate, this.onActivityPrivilegeInfoUpdateHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdate);
            NetDispatcher.addCmdListener(ServerCommand.ActiveTypeActPrevilege, this.onActiveTypeActPrevilegeHandler);
            return;
        }

        /* internal  */onAddToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ActivityPrivilegeView));
            this._module.updateActTime(this._activeToClient);
            this._module.updateList(cache.activityPrivilege.apInfoArray);
            return;
        }

        /* internal  */onActivityPrivilegeOpenHandler(arg1: DataEvent): void
        {
            view.show();
            return;
        }

        /* internal  */onActivityPrivilegeViewHandler(arg1: DataEvent): void
        {
            GameProxy.roleProxy.viewActivePrevilege();
            return;
        }

        /* internal  */onActivityPrivilegeBuyHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.roleProxy.buyActivePrevilegeGiftBag(loc1);
            return;
        }

        /* internal  */onActivityPrivilegeInfoUpdateHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            cache.activityPrivilege.buyedArray = loc1;
            if (isViewShow) 
            {
                this._module.updateList(cache.activityPrivilege.apInfoArray);
            }
            return;
        }

        /* internal  */onRoleLevelUpdate(arg1: Object): void
        {
            if (isViewShow) 
            {
                this._module.updateList(cache.activityPrivilege.apInfoArray);
            }
            return;
        }

        /* internal  */onActiveTypeActPrevilegeHandler(arg1: Object=null): void
        {
            this._activeToClient = arg1 as SActiveToClient;
            var loc1=cache.active.isActiveInEffect(EActiveType._EActiveTypeActPrevilege);
            if (loc1) 
            {
                if (isViewShow) 
                {
                    this._module.updateActTime(this._activeToClient);
                }
                ClockManager.instance.addDateCall(this._activeToClient.endDt, this.hideByActive);
                this.icon.show();
            }
            else 
            {
                this.hideByActive();
            }
            return;
        }

        /* internal  */hideByActive(): void
        {
            if (this._icon) 
            {
                this.icon.hide();
            }
            if (isViewShow) 
            {
                view.hide();
            }
            return;
        }

        private /* var */_module: IActivityPrivilegeModule;

        private /* var */_icon: ActivityPrivilegeIcon;

        private /* var */_activeToClient: SActiveToClient;
    }
